package iot_aliyun

import (
	"encoding/json"
	"fmt"
	"gitee.com/captials-team/ubdframe/src/common/utils"
	"io/ioutil"
)

// ThingModel 阿里云平台的物模型结构
type ThingModel struct {
	//额外的iot信息，不包含在阿里物模型中
	IotExtra IotExtraInfo `json:"iot_extra"`

	Schema string `json:"schema"` //物模型结构定义的访问URL

	Profile ThingProfile `json:"profile"`

	//属性
	Properties []ThingProperty `json:"properties"`

	//事件
	Events []ThingEvent `json:"events"`

	//服务
	Services []ThingService `json:"services"`
}

type ThingProfile struct {
	Version    string `json:"version"`    //当前版本
	ProductKey string `json:"productKey"` //当前产品的ProductKey
}

// ThingBase 基本信息
type ThingBase struct {
	Identifier string `json:"identifier"` // 属性/事件/服务唯一标识符（物模型模块下唯一）
	Name       string `json:"name"`       // 属性/事件/服务名称
	Desc       string `json:"desc"`       // 属性/事件/服务描述
	Required   bool   `json:"required"`   //是否是标准功能的必选事件：是（true），否（false）。
}

func (d ThingBase) String() string {
	s, _ := json.Marshal(d)
	return string(s)
}

type ThingProperty struct {
	ThingBase  `json:",inline"`
	DataType   ThingDataType `json:"dataType"`   // 参数的数据类型
	AccessMode string        `json:"accessMode"` //属性读写类型：只读（r）或读写（rw）。
}

func (d ThingProperty) String() string {
	s, _ := json.Marshal(d)
	return string(s)
}

type ThingEvent struct {
	ThingBase  `json:",inline"`
	Method     string          `json:"method"`     //事件对应的方法名称
	Type       string          `json:"type"`       //事件类型（info、alert、error）
	OutputData []ThingDataItem `json:"outputData"` //事件输出数据
}

func (d ThingEvent) String() string {
	s, _ := json.Marshal(d)
	return string(s)
}

type ThingService struct {
	ThingBase  `json:",inline"`
	CallType   string          `json:"callType"` //调用方式 sync/async
	Method     string          `json:"method"`
	InputData  []ThingDataItem `json:"inputData"`
	OutputData []ThingDataItem `json:"outputData"`
}

func (d ThingService) String() string {
	s, _ := json.Marshal(d)
	return string(s)
}

// ThingDataItem 子数据项
type ThingDataItem struct {
	Identifier string        `json:"identifier"` // 参数唯一标识符
	Name       string        `json:"name"`       // 参数名称
	DataType   ThingDataType `json:"dataType"`   // 参数的数据类型
}

// ThingDataType 数据类型
type ThingDataType struct {
	Type  string    `json:"type"` //元素的类型（可以为array类型特有）
	Specs IDataSpec `json:"specs"`
}

func (d ThingDataType) String() string {
	s, _ := json.Marshal(d.Specs)
	return fmt.Sprintf("type=%s,specs=%s", d.Type, string(s))
}

type IotExtraInfo struct {
	FuncType string `json:"funcType"` //设备类型，如BMS/PCS等
}

//// ThingDataTypeSpecs 数据类型描述信息
//type ThingDataTypeSpecs struct {
//	Field0   string `json:"0"`        //0的值（bool类型特有）
//	Field1   string `json:"1"`        //1的值（bool类型特有）
//	Min      string `json:"min"`      //参数最小值（int、float、double类型特有）
//	Max      string `json:"max"`      //参数最大值（int、float、double类型特有）
//	Unit     string `json:"unit"`     //属性单位（int、float、double类型特有，非必填）,如 %
//	UnitName string `json:"unitName"` //单位名称（int、float、double类型特有，非必填）,如 百分比
//	Size     string `json:"size"`     //数组元素的个数，最大512（array类型特有）
//	Step     string `json:"step"`     //步长（text、enum类型无此参数）
//	Length   string `json:"length"`   //数据长度，最大10240（text类型特有）
//	Item     struct {
//		Type string `json:"type"` //数组元素的类型（array类型特有）
//	} `json:"item"`
//}

func Parse(s string) ThingModel {
	var m ThingModel
	json.Unmarshal([]byte(s), &m)

	for i := 1; i <= 2; i++ {
		for k, v := range m.Properties {
			m.Properties[k].DataType = ParseDataSpecs(v.DataType)
		}
		for k, v := range m.Events {
			for kk, vv := range v.OutputData {
				m.Events[k].OutputData[kk].DataType = ParseDataSpecs(vv.DataType)
			}
		}
		for k, v := range m.Services {
			for kk, vv := range v.OutputData {
				m.Services[k].OutputData[kk].DataType = ParseDataSpecs(vv.DataType)
			}
			for kk, vv := range v.InputData {
				m.Services[k].InputData[kk].DataType = ParseDataSpecs(vv.DataType)
			}
		}
		json.Unmarshal([]byte(s), &m)
	}

	return m
}

func ParseFile(file string) (ThingModel, error) {
	s := ""
	if !utils.FilePathExist(file) {
		return ThingModel{}, fmt.Errorf("file not exist %s", file)
	}
	tmp, err := ioutil.ReadFile(file)
	if err != nil {
		return ThingModel{}, err
	}
	s = string(tmp)
	return Parse(s), nil
}

// ParseDataSpecs 解析dataSpecs，对于Specs字段未解析的情况
func ParseDataSpecs(data ThingDataType) ThingDataType {
	var ret ThingDataType = data
	//s, _ := json.Marshal(data)
	if ret.Specs == nil {
		//fmt.Printf("dataType=%+v\n", ret.Specs)
		for _, v := range DataSpecList {
			if v.TypeName() == data.Type {
				ret.Specs = v.Copy()
				break
			}
		}
	}

	if tmp, ok := ret.Specs.(*StructData); ok {
		//fmt.Printf("%s-%v\n", ret.Type, ret.Specs)
		for k, v := range *tmp {
			//fmt.Printf("\t%+v\n", v)
			(*tmp)[k].DataType = ParseDataSpecs(v.DataType)
		}
	}
	if tmp, ok := ret.Specs.(*ArrayData); ok {
		//fmt.Printf("%s-%v\n", ret.Type, ret.Specs)
		//fmt.Printf("\t%+v\n", v)
		tmp.Item = ParseDataSpecs(tmp.Item)
	}
	//json.Unmarshal(s, &ret)
	return ret
}

const AliSchema = "https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json"

func TestModelStr() string {
	return `
{
  "schema": "https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
  "profile": {
    "version": "1.0",
    "productKey": "k0zwuXuUSyn"
  },
  "properties": [
    {
      "identifier": "nUc",
      "name": "C相电压",
      "accessMode": "r",
      "required": false,
      "dataType": {
        "type": "float",
        "specs": {
          "min": "0",
          "max": "1000",
          "unit": "V",
          "unitName": "伏特",
          "step": "0.1"
        }
      }
    },
    {
      "identifier": "nUa",
      "name": "A相电压",
      "accessMode": "r",
      "required": false,
      "dataType": {
        "type": "double",
        "specs": {
          "min": "0",
          "max": "1000",
          "unit": "V",
          "unitName": "伏特",
          "step": "0.01"
        }
      }
    },
    {
      "identifier": "USBSwitch_1",
      "name": "USB开关1",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "bool",
        "specs": {
          "0": "关闭",
          "1": "开启"
        }
      }
    },
    {
      "identifier": "MACAddress",
      "name": "MAC地址",
      "accessMode": "r",
      "required": false,
      "dataType": {
        "type": "text",
        "specs": {
          "length": "128"
        }
      }
    },
    {
      "identifier": "nUb",
      "name": "B相电压",
      "accessMode": "r",
      "required": false,
      "dataType": {
        "type": "double",
        "specs": {
          "min": "0",
          "max": "1000",
          "unit": "V",
          "unitName": "伏特",
          "step": "0.01"
        }
      }
    },
    {
      "identifier": "CpuUsage",
      "name": "CPU使用率",
      "accessMode": "r",
      "required": false,
      "dataType": {
        "type": "double",
        "specs": {
          "min": "0",
          "max": "100",
          "unit": "%",
          "unitName": "百分比",
          "step": "0.1"
        }
      }
    },
    {
      "identifier": "Position",
      "name": "衣杆位置",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "enum",
        "specs": {
          "0": "无此功能",
          "1": "最上面",
          "2": "中间",
          "3": "最下面"
        }
      }
    },
    {
      "identifier": "SteamerTimeRemainingMinute",
      "name": "蒸箱制作剩余时间",
      "accessMode": "r",
      "required": false,
      "dataType": {
        "type": "int",
        "specs": {
          "min": "0",
          "max": "65535",
          "unit": "min",
          "unitName": "分钟",
          "step": "1"
        }
      }
    },
    {
      "identifier": "GeoLocation",
      "name": "地理位置",
      "accessMode": "r",
      "required": false,
      "dataType": {
        "type": "struct",
        "specs": [
          {
            "identifier": "Longitude",
            "name": "经度",
            "dataType": {
              "type": "double",
              "specs": {
                "min": "-180",
                "max": "180",
                "unit": "°",
                "unitName": "度",
                "step": "0.01"
              }
            }
          },
          {
            "identifier": "Latitude",
            "name": "纬度",
            "dataType": {
              "type": "double",
              "specs": {
                "min": "-90",
                "max": "90",
                "unit": "°",
                "unitName": "度",
                "step": "0.01"
              }
            }
          },
          {
            "identifier": "Altitude",
            "name": "海拔",
            "dataType": {
              "type": "double",
              "specs": {
                "min": "0",
                "max": "9999",
                "unit": "m",
                "unitName": "米",
                "step": "0.01"
              }
            }
          },
          {
            "identifier": "CoordinateSystem",
            "name": "坐标系统",
            "dataType": {
              "type": "enum",
              "specs": {
                "1": "WGS_84",
                "2": "GCJ_02"
              }
            }
          }
        ]
      }
    },
    {
      "identifier": "PeriodTimer_4",
      "name": "循环定时4",
      "accessMode": "rw",
      "desc": "请将此属性 添加到 插座 品类中，可选",
      "required": false,
      "dataType": {
        "type": "struct",
        "specs": [
          {
            "identifier": "StartTime",
            "name": "开始时间",
            "dataType": {
              "type": "text",
              "specs": {
                "length": "1024"
              }
            }
          },
          {
            "identifier": "EndTime",
            "name": "结束时间",
            "dataType": {
              "type": "text",
              "specs": {
                "length": "1024"
              }
            }
          },
          {
            "identifier": "Repeat",
            "name": "重复",
            "dataType": {
              "type": "text",
              "specs": {
                "length": "1024"
              }
            }
          },
          {
            "identifier": "Enable",
            "name": "启用",
            "dataType": {
              "type": "bool",
              "specs": {
                "0": "关",
                "1": "开"
              }
            }
          },
          {
            "identifier": "TimeZoneOffset",
            "name": "时差",
            "dataType": {
              "type": "int",
              "specs": {
                "min": "-43200",
                "max": "50400",
                "unit": "s",
                "unitName": "秒",
                "step": "1"
              }
            }
          },
          {
            "identifier": "RunTime",
            "name": "运行时间",
            "dataType": {
              "type": "int",
              "specs": {
                "min": "10",
                "max": "720",
                "unit": "min",
                "unitName": "分",
                "step": "10"
              }
            }
          },
          {
            "identifier": "SleepTime",
            "name": "间隔时间",
            "dataType": {
              "type": "int",
              "specs": {
                "min": "10",
                "max": "720",
                "unit": "min",
                "unitName": "分",
                "step": "10"
              }
            }
          }
        ]
      }
    },
    {
      "identifier": "battery_cluster",
      "name": "电池簇",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "array",
        "specs": {
          "size": "10",
          "item": {
            "type": "float"
          }
        }
      }
    }
  ],
  "events": [
    {
      "identifier": "post",
      "name": "post",
      "type": "info",
      "required": true,
      "desc": "属性上报",
      "method": "thing.event.property.post",
      "outputData": [
        {
          "identifier": "nUc",
          "name": "C相电压",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "V",
              "unitName": "伏特",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "nUa",
          "name": "A相电压",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "V",
              "unitName": "伏特",
              "step": "0.01"
            }
          }
        },
        {
          "identifier": "USBSwitch_1",
          "name": "USB开关1",
          "dataType": {
            "type": "bool",
            "specs": {
              "0": "关闭",
              "1": "开启"
            }
          }
        },
        {
          "identifier": "MACAddress",
          "name": "MAC地址",
          "dataType": {
            "type": "text",
            "specs": {
              "length": "128"
            }
          }
        },
        {
          "identifier": "nUb",
          "name": "B相电压",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "V",
              "unitName": "伏特",
              "step": "0.01"
            }
          }
        },
        {
          "identifier": "CpuUsage",
          "name": "CPU使用率",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%",
              "unitName": "百分比",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "Position",
          "name": "衣杆位置",
          "dataType": {
            "type": "enum",
            "specs": {
              "0": "无此功能",
              "1": "最上面",
              "2": "中间",
              "3": "最下面"
            }
          }
        },
        {
          "identifier": "SteamerTimeRemainingMinute",
          "name": "蒸箱制作剩余时间",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "65535",
              "unit": "min",
              "unitName": "分钟",
              "step": "1"
            }
          }
        },
        {
          "identifier": "GeoLocation",
          "name": "地理位置",
          "dataType": {
            "type": "struct",
            "specs": [
              {
                "identifier": "Longitude",
                "name": "经度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-180",
                    "max": "180",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Latitude",
                "name": "纬度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-90",
                    "max": "90",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Altitude",
                "name": "海拔",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "0",
                    "max": "9999",
                    "unit": "m",
                    "unitName": "米",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "CoordinateSystem",
                "name": "坐标系统",
                "dataType": {
                  "type": "enum",
                  "specs": {
                    "1": "WGS_84",
                    "2": "GCJ_02"
                  }
                }
              }
            ]
          }
        },
        {
          "identifier": "PeriodTimer_4",
          "name": "循环定时4",
          "dataType": {
            "type": "struct",
            "specs": [
              {
                "identifier": "StartTime",
                "name": "开始时间",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "EndTime",
                "name": "结束时间",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "Repeat",
                "name": "重复",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "Enable",
                "name": "启用",
                "dataType": {
                  "type": "bool",
                  "specs": {
                    "0": "关",
                    "1": "开"
                  }
                }
              },
              {
                "identifier": "TimeZoneOffset",
                "name": "时差",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "-43200",
                    "max": "50400",
                    "unit": "s",
                    "unitName": "秒",
                    "step": "1"
                  }
                }
              },
              {
                "identifier": "RunTime",
                "name": "运行时间",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "10",
                    "max": "720",
                    "unit": "min",
                    "unitName": "分",
                    "step": "10"
                  }
                }
              },
              {
                "identifier": "SleepTime",
                "name": "间隔时间",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "10",
                    "max": "720",
                    "unit": "min",
                    "unitName": "分",
                    "step": "10"
                  }
                }
              }
            ]
          }
        },
        {
          "identifier": "battery_cluster",
          "name": "电池簇",
          "dataType": {
            "type": "array",
            "specs": {
              "size": "10",
              "item": {
                "type": "float"
              }
            }
          }
        }
      ]
    },
    {
      "identifier": "PHAlarm",
      "name": "PH告警",
      "type": "alert",
      "required": false,
      "method": "thing.event.PHAlarm.post",
      "outputData": [
        {
          "identifier": "AlarmType",
          "name": "报警类型",
          "dataType": {
            "type": "enum",
            "specs": {
              "0": "正常",
              "1": "高",
              "2": "低"
            }
          }
        },
        {
          "identifier": "PH",
          "name": "PH",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "14",
              "step": "0.1"
            }
          }
        }
      ]
    },
    {
      "identifier": "LowVoltageNotification",
      "name": "低电压上报",
      "type": "info",
      "required": false,
      "method": "thing.event.LowVoltageNotification.post",
      "outputData": [
        {
          "identifier": "VoltageValue",
          "name": "电压值",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "3.5",
              "max": "7.0",
              "unit": "V",
              "unitName": "伏特",
              "step": "0.01"
            }
          }
        }
      ]
    },
    {
      "identifier": "ContentSdkInitCompleteEvent",
      "name": "SDK初始化完成",
      "type": "info",
      "required": false,
      "desc": "内容推荐",
      "method": "thing.event.ContentSdkInitCompleteEvent.post",
      "outputData": [
        {
          "identifier": "DeviceInfo",
          "name": "设备基本信息",
          "dataType": {
            "type": "text",
            "specs": {
              "length": "4096"
            }
          }
        }
      ]
    },
    {
      "identifier": "ContentSdkStatusEvent",
      "name": "SDK运行状态",
      "type": "info",
      "required": false,
      "desc": "内容推荐",
      "method": "thing.event.ContentSdkStatusEvent.post",
      "outputData": [
        {
          "identifier": "Status",
          "name": "状态类型值",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "-1024",
              "max": "1024",
              "unitName": "无",
              "step": "1"
            }
          }
        },
        {
          "identifier": "Message",
          "name": "消息内容",
          "dataType": {
            "type": "text",
            "specs": {
              "length": "1024"
            }
          }
        }
      ]
    }
  ],
  "services": [
    {
      "identifier": "set",
      "name": "set",
      "required": true,
      "callType": "async",
      "desc": "属性设置",
      "method": "thing.service.property.set",
      "inputData": [
        {
          "identifier": "USBSwitch_1",
          "name": "USB开关1",
          "dataType": {
            "type": "bool",
            "specs": {
              "0": "关闭",
              "1": "开启"
            }
          }
        },
        {
          "identifier": "Position",
          "name": "衣杆位置",
          "dataType": {
            "type": "enum",
            "specs": {
              "0": "无此功能",
              "1": "最上面",
              "2": "中间",
              "3": "最下面"
            }
          }
        },
        {
          "identifier": "PeriodTimer_4",
          "name": "循环定时4",
          "dataType": {
            "type": "struct",
            "specs": [
              {
                "identifier": "StartTime",
                "name": "开始时间",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "EndTime",
                "name": "结束时间",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "Repeat",
                "name": "重复",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "Enable",
                "name": "启用",
                "dataType": {
                  "type": "bool",
                  "specs": {
                    "0": "关",
                    "1": "开"
                  }
                }
              },
              {
                "identifier": "TimeZoneOffset",
                "name": "时差",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "-43200",
                    "max": "50400",
                    "unit": "s",
                    "unitName": "秒",
                    "step": "1"
                  }
                }
              },
              {
                "identifier": "RunTime",
                "name": "运行时间",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "10",
                    "max": "720",
                    "unit": "min",
                    "unitName": "分",
                    "step": "10"
                  }
                }
              },
              {
                "identifier": "SleepTime",
                "name": "间隔时间",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "10",
                    "max": "720",
                    "unit": "min",
                    "unitName": "分",
                    "step": "10"
                  }
                }
              }
            ]
          }
        },
        {
          "identifier": "battery_cluster",
          "name": "电池簇",
          "dataType": {
            "type": "array",
            "specs": {
              "size": "10",
              "item": {
                "type": "float"
              }
            }
          }
        }
      ],
      "outputData": []
    },
    {
      "identifier": "get",
      "name": "get",
      "required": true,
      "callType": "async",
      "desc": "属性获取",
      "method": "thing.service.property.get",
      "inputData": [
        "nUc",
        "nUa",
        "USBSwitch_1",
        "MACAddress",
        "nUb",
        "CpuUsage",
        "Position",
        "SteamerTimeRemainingMinute",
        "GeoLocation",
        "PeriodTimer_4",
        "battery_cluster"
      ],
      "outputData": [
        {
          "identifier": "nUc",
          "name": "C相电压",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "V",
              "unitName": "伏特",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "nUa",
          "name": "A相电压",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "V",
              "unitName": "伏特",
              "step": "0.01"
            }
          }
        },
        {
          "identifier": "USBSwitch_1",
          "name": "USB开关1",
          "dataType": {
            "type": "bool",
            "specs": {
              "0": "关闭",
              "1": "开启"
            }
          }
        },
        {
          "identifier": "MACAddress",
          "name": "MAC地址",
          "dataType": {
            "type": "text",
            "specs": {
              "length": "128"
            }
          }
        },
        {
          "identifier": "nUb",
          "name": "B相电压",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "1000",
              "unit": "V",
              "unitName": "伏特",
              "step": "0.01"
            }
          }
        },
        {
          "identifier": "CpuUsage",
          "name": "CPU使用率",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%",
              "unitName": "百分比",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "Position",
          "name": "衣杆位置",
          "dataType": {
            "type": "enum",
            "specs": {
              "0": "无此功能",
              "1": "最上面",
              "2": "中间",
              "3": "最下面"
            }
          }
        },
        {
          "identifier": "SteamerTimeRemainingMinute",
          "name": "蒸箱制作剩余时间",
          "dataType": {
            "type": "int",
            "specs": {
              "min": "0",
              "max": "65535",
              "unit": "min",
              "unitName": "分钟",
              "step": "1"
            }
          }
        },
        {
          "identifier": "GeoLocation",
          "name": "地理位置",
          "dataType": {
            "type": "struct",
            "specs": [
              {
                "identifier": "Longitude",
                "name": "经度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-180",
                    "max": "180",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Latitude",
                "name": "纬度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-90",
                    "max": "90",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Altitude",
                "name": "海拔",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "0",
                    "max": "9999",
                    "unit": "m",
                    "unitName": "米",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "CoordinateSystem",
                "name": "坐标系统",
                "dataType": {
                  "type": "enum",
                  "specs": {
                    "1": "WGS_84",
                    "2": "GCJ_02"
                  }
                }
              }
            ]
          }
        },
        {
          "identifier": "PeriodTimer_4",
          "name": "循环定时4",
          "dataType": {
            "type": "struct",
            "specs": [
              {
                "identifier": "StartTime",
                "name": "开始时间",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "EndTime",
                "name": "结束时间",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "Repeat",
                "name": "重复",
                "dataType": {
                  "type": "text",
                  "specs": {
                    "length": "1024"
                  }
                }
              },
              {
                "identifier": "Enable",
                "name": "启用",
                "dataType": {
                  "type": "bool",
                  "specs": {
                    "0": "关",
                    "1": "开"
                  }
                }
              },
              {
                "identifier": "TimeZoneOffset",
                "name": "时差",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "-43200",
                    "max": "50400",
                    "unit": "s",
                    "unitName": "秒",
                    "step": "1"
                  }
                }
              },
              {
                "identifier": "RunTime",
                "name": "运行时间",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "10",
                    "max": "720",
                    "unit": "min",
                    "unitName": "分",
                    "step": "10"
                  }
                }
              },
              {
                "identifier": "SleepTime",
                "name": "间隔时间",
                "dataType": {
                  "type": "int",
                  "specs": {
                    "min": "10",
                    "max": "720",
                    "unit": "min",
                    "unitName": "分",
                    "step": "10"
                  }
                }
              }
            ]
          }
        },
        {
          "identifier": "battery_cluster",
          "name": "电池簇",
          "dataType": {
            "type": "array",
            "specs": {
              "size": "10",
              "item": {
                "type": "float"
              }
            }
          }
        }
      ]
    },
    {
      "identifier": "Reboot",
      "name": "Reboot",
      "required": false,
      "callType": "async",
      "method": "thing.service.Reboot",
      "inputData": [],
      "outputData": []
    },
    {
      "identifier": "ClearRuntimeData",
      "name": "ClearRuntimeData",
      "required": false,
      "callType": "sync",
      "method": "thing.service.ClearRuntimeData",
      "inputData": [],
      "outputData": [
        {
          "identifier": "Result",
          "name": "Result",
          "dataType": {
            "type": "enum",
            "specs": {
              "0": "Success",
              "1": "Failed"
            }
          }
        }
      ]
    }
  ]
}
`
}
